﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	internal class ClassParallelPipeXMLInfo
	{
		public static void InitializeFilePath(string path, string rootNode)
		{
			if (!File.Exists(path))
			{
				if (File.Exists(Path.Combine(Product.DataLocation, "User\\HYDrawParallelPipeInfo.xml")))
				{
					File.Copy(Path.Combine(Product.DataLocation, "User\\HYDrawParallelPipeInfo.xml"), path);
					return;
				}
				XmlDocument xmlDocument = new XmlDocument();
				XmlDeclaration newChild = xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null);
				xmlDocument.AppendChild(newChild);
				XmlElement newChild2 = xmlDocument.CreateElement(rootNode);
				xmlDocument.AppendChild(newChild2);
				xmlDocument.Save(path);
			}
		}

		public static void writestruct(string path, string rootNodeName, List<drawPipeStruct> listStruct)
		{
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNode xmlNode = xmlDocument.SelectSingleNode(rootNodeName);
			xmlNode.RemoveAll();
			XmlNodeList childNodes = xmlDocument.SelectSingleNode(rootNodeName).ChildNodes;
			if (childNodes.Count > 0)
			{
				foreach (drawPipeStruct drawPipeStruct in listStruct)
				{
					XmlElement xmlElement = ClassParallelPipeXMLInfo.JustXmlNodelistHasElement(childNodes, drawPipeStruct.pipingSystemName);
					if (xmlElement != null)
					{
						xmlElement.Attributes["isDraw"].Value = drawPipeStruct.IsDraw.ToString();
						xmlElement.Attributes["levelOffSer"].Value = drawPipeStruct.levelOffSet.ToString();
						xmlElement.Attributes["offSet"].Value = drawPipeStruct.offSet.ToString();
						xmlElement.Attributes["pipeDiameter"].Value = drawPipeStruct.pipeDiameter.ToString();
						xmlElement.Attributes["pipeType"].Value = drawPipeStruct.pipeTypeName;
					}
					else
					{
						XmlNode xmlNode2 = xmlDocument.CreateElement("add");
						(xmlNode2 as XmlElement).SetAttribute("isDraw", drawPipeStruct.IsDraw.ToString());
						(xmlNode2 as XmlElement).SetAttribute("name", drawPipeStruct.pipingSystemName);
						(xmlNode2 as XmlElement).SetAttribute("levelOffSer", drawPipeStruct.levelOffSet.ToString());
						(xmlNode2 as XmlElement).SetAttribute("offSet", drawPipeStruct.offSet.ToString());
						(xmlNode2 as XmlElement).SetAttribute("pipeDiameter", drawPipeStruct.pipeDiameter.ToString());
						(xmlNode2 as XmlElement).SetAttribute("pipeType", drawPipeStruct.pipeTypeName);
						xmlNode.AppendChild(xmlNode2);
					}
				}
				childNodes = xmlDocument.SelectSingleNode(rootNodeName).ChildNodes;
				for (int i = 0; i < childNodes.Count; i++)
				{
					XmlElement xmlElement2 = ClassParallelPipeXMLInfo.JustXmlnodeIsDelete(childNodes[i], listStruct);
					if (xmlElement2 != null)
					{
						xmlNode.RemoveChild(xmlElement2);
					}
				}
			}
			else
			{
				foreach (drawPipeStruct drawPipeStruct2 in listStruct)
				{
					XmlNode xmlNode3 = xmlDocument.CreateElement("add");
					(xmlNode3 as XmlElement).SetAttribute("isDraw", drawPipeStruct2.IsDraw.ToString());
					(xmlNode3 as XmlElement).SetAttribute("name", drawPipeStruct2.pipingSystemName);
					(xmlNode3 as XmlElement).SetAttribute("levelOffSer", drawPipeStruct2.levelOffSet.ToString());
					(xmlNode3 as XmlElement).SetAttribute("offSet", drawPipeStruct2.offSet.ToString());
					(xmlNode3 as XmlElement).SetAttribute("pipeDiameter", drawPipeStruct2.pipeDiameter.ToString());
					(xmlNode3 as XmlElement).SetAttribute("pipeType", drawPipeStruct2.pipeTypeName);
					xmlNode.AppendChild(xmlNode3);
				}
			}
			xmlDocument.Save(path);
		}

		public static List<drawPipeStruct> raedInfoFromXML(string path, string rootNodeName)
		{
			List<drawPipeStruct> list = new List<drawPipeStruct>();
			XmlDocument xmlDocument = new XmlDocument();
			xmlDocument.Load(path);
			XmlNodeList childNodes = xmlDocument.SelectSingleNode(rootNodeName).ChildNodes;
			if (childNodes.Count > 0)
			{
				foreach (object obj in childNodes)
				{
					XmlElement xmlElement = (XmlElement)((XmlNode)obj);
					list.Add(new drawPipeStruct
					{
						IsDraw = bool.Parse(xmlElement.Attributes["isDraw"].Value),
						pipingSystemName = xmlElement.Attributes["name"].Value,
						levelOffSet = double.Parse(xmlElement.Attributes["levelOffSer"].Value),
						offSet = double.Parse(xmlElement.Attributes["offSet"].Value),
						pipeDiameter = double.Parse(xmlElement.Attributes["pipeDiameter"].Value),
						pipeTypeName = xmlElement.Attributes["pipeType"].Value
					});
				}
			}
			return list;
		}

		private static XmlElement JustXmlNodelistHasElement(XmlNodeList nodelist, string szName)
		{
			foreach (object obj in nodelist)
			{
				XmlElement xmlElement = (XmlElement)((XmlNode)obj);
				if (xmlElement.Attributes["name"].Value == szName)
				{
					return xmlElement;
				}
			}
			return null;
		}

		private static XmlElement JustXmlnodeIsDelete(XmlNode node, List<drawPipeStruct> listStruct)
		{
			XmlElement xmlElement = (XmlElement)node;
			List<string> list = new List<string>();
			foreach (drawPipeStruct drawPipeStruct in listStruct)
			{
				list.Add(drawPipeStruct.pipingSystemName);
			}
			if (list.Contains(xmlElement.Attributes["name"].Value))
			{
				return null;
			}
			return xmlElement;
		}
	}
}
